//
//  TOAddPicView.m
//  X-Tribe
//
//  Created by Miss.Ou on 16/4/7.
//  Copyright © 2016年 Miss.Ou. All rights reserved.
//

#import "TOAddPicView.h"

@implementation TOAddPicView



-(void)addImage:(UIImage*)image
{
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    imageView.image = image;
    [self addSubview:imageView];
    
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    long count = self.subviews.count;
    //一行的最大列数
    int maxColsPerRow = 4;
    //每个图片之间的间距
    CGFloat margin = 10;
    
    //每个图片的宽高
    CGFloat imageViewW = (self.width - (maxColsPerRow + 1) * margin) / maxColsPerRow;
    CGFloat imageViewH = imageViewW;
    
    for (int i = 0; i < count; i++)
    {
        //行号
        int row = i / maxColsPerRow;
        //列号
        int col = i % maxColsPerRow;
        
        UIImageView *imageView = self.subviews[i];
        imageView.width = imageViewW;
        imageView.height = imageViewH;
        imageView.y = row * (imageViewH + margin);
        imageView.x = col * (imageViewW + margin) + margin;
    }
    
}

-(NSArray *)images
{
    
    NSMutableArray *array = [NSMutableArray array];
    for (UIImageView * imageView in self.subviews) {
        [array addObject:imageView.image];
        
    }
    return array;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
